package com.xydtech.ecard.config.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.ecard.config.domain.EcardNewsConfig;
import com.xydtech.ecard.config.service.IEcardNewsConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 新闻设置Controller
 *
 * @author JHON
 * @date 2022-04-12
 */
@Controller
@RequestMapping("/config/config")
public class EcardNewsConfigController extends BaseController {
    private String prefix = "config/config";

    @Autowired
    private IEcardNewsConfigService ecardNewsConfigService;

    @RequiresPermissions("config:config:view")
    @GetMapping()
    public String config() {
        return prefix + "/config";
    }

    /**
     * 查询新闻设置列表
     */
    @RequiresPermissions("config:config:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardNewsConfig ecardNewsConfig) {
        startPage();
        List<EcardNewsConfig> list = ecardNewsConfigService.selectEcardNewsConfigList(ecardNewsConfig);
        return getDataTable(list);
    }

    /**
     * 导出新闻设置列表
     */
    @RequiresPermissions("config:config:export")
    @Log(title = "新闻设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EcardNewsConfig ecardNewsConfig) {
        List<EcardNewsConfig> list = ecardNewsConfigService.selectEcardNewsConfigList(ecardNewsConfig);
        ExcelUtil<EcardNewsConfig> util = new ExcelUtil<EcardNewsConfig>(EcardNewsConfig.class);
        return util.exportExcel(list, "新闻设置数据");
    }

    /**
     * 新增新闻设置
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存新闻设置
     */
    @RequiresPermissions("config:config:add")
    @Log(title = "新闻设置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EcardNewsConfig ecardNewsConfig) {
        int i = 0;
        try {
            i = ecardNewsConfigService.insertEcardNewsConfig(ecardNewsConfig);
        } catch (Exception e) {
            //处理唯一索引异常，捕获并抛出
            Throwable cause = e.getCause();
            if (cause instanceof java.sql.SQLIntegrityConstraintViolationException) {
                //错误信息
                String errMsg = ((java.sql.SQLIntegrityConstraintViolationException) cause).getMessage();
                //根据约束名称定位是那个字段
                if (StringUtils.isNotBlank(errMsg) && errMsg.indexOf("ecard_news_config_index_1") != -1) {
                    return AjaxResult.warn("当前机构下已存在数据！");
                }
            }
        }
        return toAjax(i);
    }

    /**
     * 修改新闻设置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        EcardNewsConfig ecardNewsConfig = ecardNewsConfigService.selectEcardNewsConfigById(id);
        mmap.put("ecardNewsConfig", ecardNewsConfig);
        return prefix + "/edit";
    }

    /**
     * 修改保存新闻设置
     */
    @RequiresPermissions("config:config:edit")
    @Log(title = "新闻设置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EcardNewsConfig ecardNewsConfig) {
        return toAjax(ecardNewsConfigService.updateEcardNewsConfig(ecardNewsConfig));
    }

    /**
     * 删除新闻设置
     */
    @RequiresPermissions("config:config:remove")
    @Log(title = "新闻设置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ecardNewsConfigService.deleteEcardNewsConfigByIds(ids));
    }
}
